'''
@Project ：hdcms-scripts
@File    ：db_util.py
@IDE     ：PyCharm
@Author  ：朱广杰
@Date    ：2024/11/8 16:32
'''
from common.setting import config
import pymysql

class DB_Util:

   def __init__(self, host, user, password, port, db):
       self.connect = pymysql.Connect(
           host=host,
           user=user,
           password=password,
           port=port,
           db=db,
           charset='utf8mb4',
           cursorclass=pymysql.cursors.DictCursor
       )

   def select(self, sql):
       cursor = self.connect.cursor()  # 相当于拿到了数据库数据集
       cursor.execute(sql)
       result = cursor.fetchall()
       cursor.close()
       self.connect.commit()  # 如果不提交数据，那么如果数据有更新，那么下次查询还是旧数据
       return result

   def update(self, sql):
       cursor = self.connect.cursor()
       cursor.execute(sql)
       self.connect.commit()
       cursor.close()

   def close(self):
       if self.connect is not None:
           self.connect.close()

if __name__ == '__main__':
   # course_db = config.Config.course_db_config
   # db_util = DB_Util(host=course_db['host'], user=course_db['user'], password=course_db['password'], port=int(course_db['port']), db=course_db['db'])
   # data = db_util.select(sql="SELECT * FROM `course` WHERE course_id = '000922776df1db0691070d8e7333bfc8' LIMIT 0,1")
   # print(data)
    bll_course_db = config.Config.bll_course_db_config
    db_util = DB_Util(host=bll_course_db['host'], user=bll_course_db['user'], password=bll_course_db['password'],
    port=int(bll_course_db['port']), db=bll_course_db['db'])
    sql = f"SELECT discuss_room_id, bind_uid FROM `bll_course`.`discuss_room_user` WHERE `discuss_id` = '1839204818225983488' "
    data = db_util.select(sql=sql)
    # discuss_id = [item['id'] for item in data][0]
    print(data)
